package com.yksj.healthtalk.ui.salon;

import org.apache.commons.lang.math.NumberRange;
import org.apache.commons.lang.math.NumberUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
import com.yksj.healthtalk.utils.ViewFinder;
import com.yksj.healthtalk.utils.ViewUtils;



/**
 * 门票设置
 *
 */
public class SalonTicketActivity extends BaseFragmentActivity implements OnClickListener, OnCheckedChangeListener {
	String groupId;
	JSONObject mJsonObject;
	EditText dayEditText;
	EditText monEditText;
	ToggleButton mToggleButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.salon_ticket);
		
		if(savedInstanceState != null){
			groupId = savedInstanceState.getString("groupId");
		}else{
			groupId = getIntent().getStringExtra("groupId");
		}
		
		initUI();
		initDate();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("groupId", groupId);
	}

	private void initUI(){
		initTitle();
		titleTextV.setText("门票设置");
		setBackBtn("返回",this);
		setRightBtnYellowBg("提交",this);
		dayEditText = (EditText)findViewById(R.id.day_et);
		monEditText = (EditText)findViewById(R.id.month_et);
		mToggleButton = (ToggleButton)findViewById(R.id.set_on_off);
		mToggleButton.setOnClickListener(this);//
		mToggleButton.setOnCheckedChangeListener(this);
		mToggleButton.setChecked(true);
		findViewById(R.id.special_peoples).setOnClickListener(this);
	}
	
	private void initDate() {
		HttpRestClient.doHttpQuerySalonPrice(groupId,"findSalonTicketMsg",new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode,
					JSONObject response) {
				if(response.containsKey("error_message")){
					ToastUtil.showLong(getApplicationContext(),response.getString("error_message"));
				}else{
					mJsonObject = response;
					onParseData();
				}
			}
		});
	}
	
	private void onParseData(){
		JSONArray array = mJsonObject.getJSONArray("ticket");
		if(array != null && array.size() != 0){
			mToggleButton.setChecked(true);
			dayEditText.setText(array.getJSONObject(0).getString("charge"));
			monEditText.setText(array.getJSONObject(1).getString("charge"));
		}else{
			mToggleButton.setChecked(false);
		}
		dayEditText.setHint(mJsonObject.getString("dayMinCharge")+"-"+mJsonObject.getString("dayMaxCharge"));
		monEditText.setHint(mJsonObject.getString("monMinCharge")+"-"+mJsonObject.getString("monMaxCharge"));
	}
	
	/**
	 * 提交
	 */
	private void onSubmit(){
		if(!mToggleButton.isChecked()){
			JSONArray jsonArray = new JSONArray();
			mJsonObject.put("ticket",jsonArray);
			RequestParams params = new RequestParams();
			params.put("type","salonTicketSet");
			params.put("parameter", mJsonObject.toJSONString());
			HttpRestClient.doHttpSalonSpecialPriceGroupSet(params,
					new JsonsfHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, JSONObject response) {
					super.onSuccess(statusCode, response);
					ToastUtil.showShort("操作成功");
				}
			});
		}else{
				if(mJsonObject == null || !mToggleButton.isChecked()) return;
				String str = dayEditText.getText().toString();
				NumberRange range = new NumberRange(mJsonObject.getIntValue("dayMinCharge"), mJsonObject.getIntValue("dayMaxCharge"));
				if(!NumberUtils.isNumber(str) || !range.containsFloat(NumberUtils.toFloat(str))){
					ToastUtil.showLong(this,"日票设置错误"+mJsonObject.getString("dayMinCharge")+"-"+mJsonObject.getString("dayMaxCharge"));
					return;
				}
				String str1 = monEditText.getText().toString();
				range = new NumberRange(mJsonObject.getIntValue("monMinCharge"), mJsonObject.getIntValue("monMaxCharge"));
				if(!NumberUtils.isNumber(str1) || !range.containsFloat(NumberUtils.toFloat(str1))){
					ToastUtil.showLong(this,"月票设置错误"+mJsonObject.getString("monMinCharge")+"-"+mJsonObject.getString("monMaxCharge"));
					return;
				}
				JSONObject object = new JSONObject();
				object.put("ticketType","1");
				object.put("charge", str);
				
				JSONObject object1 = new JSONObject();
				object1.put("ticketType","2");
				object1.put("charge", str1);
				
				JSONArray jsonArray = new JSONArray();
				jsonArray.add(object);
				jsonArray.add(object1);
				mJsonObject.put("ticket",jsonArray);
				RequestParams params = new RequestParams();
				params.put("type","salonTicketSet");
				params.put("parameter", mJsonObject.toJSONString());
				HttpRestClient.doHttpSalonSpecialPriceGroupSet(params,new JsonsfHttpResponseHandler(this){
					@Override
					public void onSuccess(int statusCode, JSONObject response) {
						super.onSuccess(statusCode, response);
						ToastUtil.showShort("操作成功");
					}
				});
				}
		}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			onSubmit();
			break;
		case R.id.set_on_off://关闭
			if(mJsonObject != null && !mToggleButton.isChecked()){
				mJsonObject.put("ticket",new JSONArray());
			}
			break;
		case R.id.special_peoples://特殊收费人群
			Intent intent = new Intent(this,SalonSpecialPeoplesGroupListActivity.class);
			intent.putExtra("groupid",groupId);
			startActivity(intent);
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		if(arg1){
findViewById(R.id.line_lyout).setVisibility(View.VISIBLE);
		}else{
			findViewById(R.id.line_lyout).setVisibility(View.GONE);	
		}
		
	}
	
}
